package com.tql.pandian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.CalculatedValueUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.datasource.annotation.Master;
import com.ruoyi.common.datasource.annotation.Slave;
import com.ruoyi.system.api.domain.SysDictData;
import com.tql.pandian.entity.*;
import com.tql.pandian.mapper.CommonMapper;
import com.tql.pandian.vo.*;
import com.tql.pandian.mapper.TqlStapProlossMapper;
import com.tql.pandian.service.TqlStapProlossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【tql_stap_proloss】的数据库操作Service实现
* @createDate 2024-02-23 16:51:14
*/
@Service
public class TqlStapProlossServiceImpl extends ServiceImpl<TqlStapProlossMapper, TqlStapProloss> implements TqlStapProlossService{

    @Autowired
    private CommonMapper commonMapper;


    @Autowired
    private TqlStapProlossMapper tqlStapProlossMapper;

    @Override
    public List<TopCateStapProlossVO> selectTopcatePage(Map<String, Object> params) {

        /**

         select sum(tt.inAmout) as realCostCurrent,
         sum(tt.outAmout) as standardCostCurrent,
         sum(prolossAmout) as prolossCostCurrent,
         tt.stapTypeName as pname,
         tt.stapTypeCode as stapCode

         from(

         select p.inAmout,outAmout,prolossAmout,t.stap_type_name as stapTypeName,t.stap_type_code  as stapTypeCode
         from tql_stap_proloss p
         left join tql_s
         **/

        String pandianCode= (String) params.get("pandianCode");
        String deptCode= (String) params.get("deptCode");
        String fnumber = StringUtils.getPandianCodeInfo(pandianCode, 1);
       // String shopCode= (String) params.get("shopCode");
        List<TopCateStapProlossVO> thisList= commonMapper.findProloss(pandianCode,fnumber,deptCode);
        if(CollectionUtils.isEmpty(thisList)) return null;
        String lastPandianCode=commonMapper.findLastPandianCode(pandianCode);
        List<TopCateStapProlossVO> lastList=null;
        if(!Objects.isNull(lastPandianCode))
        {
            lastList= commonMapper.findProloss(lastPandianCode,fnumber,deptCode);
        }
        if(!CollectionUtils.isEmpty(lastList))
        {
            for(TopCateStapProlossVO thisVo:thisList)
            {
               String thisStapCode= thisVo.getStapCode();
               for(TopCateStapProlossVO lastVo:lastList)
               {
                   if(thisStapCode.equals(lastVo.getStapCode()))
                   {
                       thisVo.setRealCostUpper(lastVo.getRealCostCurrent());
                       thisVo.setStandardCostUpper(lastVo.getStandardCostCurrent());
                       thisVo.setProlossCostUpper(lastVo.getProlossCostCurrent());
                   }
               }
            }
        }
return thisList;







//        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
//        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());
//
//        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);
//
//        List<TqlStapProloss> upperVoList;
//        if (upperPeriod !=null) {
//            params.replace("pandianCode",upperPeriod.getPandianCode());
//            upperVoList = baseMapper.selectListByStapType(params);
//        } else {
//            upperVoList = new ArrayList<>();
//        }
//        if (voList ==null) {
//            throw new ServiceException("该门店暂无盘点信息！");
//        }
//        List<TopCateStapProlossVO> datas = new ArrayList<>();
//        Map<String, List<TqlStapProloss>> groupedMap= voList.stream()
//                .collect(Collectors.groupingBy(TqlStapProloss::getStapType));
//        groupedMap.forEach((stapType, stapTypeGroup) -> {
//            TopCateStapProlossVO data = new TopCateStapProlossVO();
//            BigDecimal realCostCurrent = BigDecimal.ZERO;
//            BigDecimal realCostUpper = BigDecimal.ZERO;
//
//            BigDecimal standardCostCurrent = BigDecimal.ZERO;
//            BigDecimal standardCostUpper = BigDecimal.ZERO;
//
//            BigDecimal prolossCostCurrent = BigDecimal.ZERO;
//            BigDecimal prolossCostUpper = BigDecimal.ZERO;
//
//            BigDecimal prolossRateCurrent = BigDecimal.ZERO;
//            BigDecimal prolossRateUpper = BigDecimal.ZERO;
//            //实际金额
//            for (TqlStapProloss compute : stapTypeGroup) {
//                realCostCurrent = realCostCurrent.add(compute.getInAmout());
//                standardCostCurrent = standardCostCurrent.add(compute.getOutAmout());
//                prolossCostCurrent = prolossCostCurrent.add(compute.getProlossAmout());
//                prolossRateCurrent = prolossRateCurrent.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
//            }
//            if (upperPeriod !=null && !upperVoList.isEmpty()) {
//                List<TqlStapProloss> upperVoListChild = upperVoList.stream().filter(x->x.getStapType().equals(stapType)).collect(Collectors.toList());
//                if (!upperVoListChild.isEmpty()) {
//                    for (TqlStapProloss compute : upperVoListChild) {
//                        realCostUpper = realCostUpper.add(compute.getInAmout());
//                        standardCostUpper = standardCostUpper.add(compute.getOutAmout());
//                        prolossCostUpper = prolossCostUpper.add(compute.getProlossAmout());
//                        prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
//                    }
//                }
//            }
//            data.setStapType(stapType);
//            data.setPName(baseMapper.getStatTypeName(stapType));
//            data.setRealCostCurrent(realCostCurrent);
//            data.setRealCostUpper(realCostUpper);
//            data.setStandardCostCurrent(standardCostCurrent);
//            data.setStandardCostUpper(standardCostUpper);
//            data.setProlossCostCurrent(prolossCostCurrent);
//            data.setProlossCostUpper(prolossCostUpper);
//            data.setProlossRateCurrent(prolossRateCurrent);
//            data.setProlossRateUpper(prolossRateUpper);
//            datas.add(data);
//        });
//        return datas;
    }

    public BigDecimal divisionFloat(BigDecimal a, BigDecimal b){
        return CalculatedValueUtil.divisionFloat(a, b);
    }
    @Override
    public List<TopCateStapProlossVO> selectTopcateDeptPage(Map<String, Object> params) {
        String pandianCode= (String) params.get("pandianCode");
        String stapType= (String) params.get("stapType");
        String fnumber = StringUtils.getPandianCodeInfo(pandianCode, 1);

        List<TopCateStapProlossVO> thisList= commonMapper.findProlossGroupDept(pandianCode,fnumber,stapType);
        if(CollectionUtils.isEmpty(thisList)) return null;
        List<TopCateStapProlossVO> lastList=null;
        String lastPandianCode=commonMapper.findLastPandianCode(pandianCode);
        if(!Objects.isNull(lastPandianCode))
        {
            lastList= commonMapper.findProlossGroupDept(lastPandianCode,fnumber,stapType );
        }
        if(!CollectionUtils.isEmpty(lastList))
        {
            for(TopCateStapProlossVO thisVo:thisList)
            {
                String thisDeptCode= thisVo.getDeptCode();
                for(TopCateStapProlossVO lastVo:lastList)
                {
                    if(thisDeptCode.equals(lastVo.getDeptCode()))
                    {
                        thisVo.setRealCostUpper(lastVo.getRealCostCurrent());
                        thisVo.setStandardCostUpper(lastVo.getStandardCostCurrent());
                        thisVo.setProlossCostUpper(lastVo.getProlossCostCurrent());
                    }
                }
            }
        }
        return thisList;




//        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
//        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());
//
//        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);
//
//        List<TqlStapProloss> upperVoList;
//        if (upperPeriod !=null) {
//            upperVoList = baseMapper.selectListByStapType(params);
//        } else {
//            upperVoList = new ArrayList<>();
//        }
//        if (voList ==null) {
//            throw new ServiceException("该门店暂无盘点信息！");
//        }
//        List<TopCateStapProlossVO> datas = new ArrayList<>();
//        Map<String, List<TqlStapProloss>> groupedMap= voList.stream()
//                .collect(Collectors.groupingBy(TqlStapProloss::getDeptCode));
//        groupedMap.forEach((deptCode, stapTypeGroup) -> {
//            TopCateStapProlossVO data = new TopCateStapProlossVO();
//            BigDecimal realCostCurrent = BigDecimal.ZERO;
//            BigDecimal realCostUpper = BigDecimal.ZERO;
//
//            BigDecimal standardCostCurrent = BigDecimal.ZERO;
//            BigDecimal standardCostUpper = BigDecimal.ZERO;
//
//            BigDecimal prolossCostCurrent = BigDecimal.ZERO;
//            BigDecimal prolossCostUpper = BigDecimal.ZERO;
//
//            BigDecimal prolossRateCurrent = BigDecimal.ZERO;
//            BigDecimal prolossRateUpper = BigDecimal.ZERO;
//            //实际金额
//            for (TqlStapProloss compute : stapTypeGroup) {
//                realCostCurrent = realCostCurrent.add(compute.getInAmout());
//                standardCostCurrent = standardCostCurrent.add(compute.getOutAmout());
//                prolossCostCurrent = prolossCostCurrent.add(compute.getProlossAmout());
//                prolossRateCurrent = prolossRateCurrent.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
//            }
//            if (upperPeriod !=null && !upperVoList.isEmpty()) {
//                List<TqlStapProloss> upperVoListChild = upperVoList.stream().filter(x->x.getDeptCode().equals(deptCode)).collect(Collectors.toList());
//                if (!upperVoListChild.isEmpty()) {
//                    for (TqlStapProloss compute : upperVoListChild) {
//                        realCostUpper = realCostUpper.add(compute.getInAmout());
//                        standardCostUpper = standardCostUpper.add(compute.getOutAmout());
//                        prolossCostUpper = prolossCostUpper.add(compute.getProlossAmout());
//                        prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
//                    }
//                }
//            }
//            data.setDeptCode(deptCode);
//            data.setPName(baseMapper.getDeptName(deptCode));
//            data.setRealCostCurrent(realCostCurrent);
//            data.setRealCostUpper(realCostUpper);
//            data.setStandardCostCurrent(standardCostCurrent);
//            data.setStandardCostUpper(standardCostUpper);
//            data.setProlossCostCurrent(prolossCostCurrent);
//            data.setProlossCostUpper(prolossCostUpper);
//            data.setProlossRateCurrent(prolossRateCurrent);
//            data.setProlossRateUpper(prolossRateUpper);
//            datas.add(data);
//        });
//        return datas;
    }

    @Override
    public List<TopCateStapProlossVO> selectPage(Map<String, Object> params) {
        String pandianCode= (String) params.get("pandianCode");
        String stapType= (String) params.get("stapTypeCode");
        String fnumber = StringUtils.getPandianCodeInfo(pandianCode, 1);

        List<TopCateStapProlossVO> thisList= commonMapper.findProlossGroupStap(pandianCode,fnumber,stapType);
        if(CollectionUtils.isEmpty(thisList)) return null;
        List<TopCateStapProlossVO> lastList=null;
        String lastPandianCode=commonMapper.findLastPandianCode(pandianCode);
        if(!Objects.isNull(lastPandianCode))
        {
            lastList= commonMapper.findProlossGroupStap
                    (lastPandianCode,fnumber,stapType );
        }
        if(!CollectionUtils.isEmpty(lastList))
        {
            for(TopCateStapProlossVO thisVo:thisList)
            {
                String thisDeptCode= thisVo.getDeptCode();
                for(TopCateStapProlossVO lastVo:lastList)
                {
                    if(thisDeptCode.equals(lastVo.getDeptCode()))
                    {
                        thisVo.setRealCostUpper(lastVo.getRealCostCurrent());
                        thisVo.setStandardCostUpper(lastVo.getStandardCostCurrent());
                        thisVo.setProlossCostUpper(lastVo.getProlossCostCurrent());
                    }
                }
            }
        }
        return thisList;


//        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
//        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());
//
//        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);
//
//        List<TqlStapProloss> upperVoList;
//        if (upperPeriod !=null) {
//            upperVoList = baseMapper.selectListByStapType(params);
//        } else {
//            upperVoList = new ArrayList<>();
//        }
//        if (voList ==null) {
//            throw new ServiceException("该门店暂无盘点信息！");
//        }
//        List<TopCateStapProlossVO> datas = new ArrayList<>();
//        TopCateStapProlossVO data;
//        for (TqlStapProloss vo : voList) {
//            data = new TopCateStapProlossVO();
//            BigDecimal realCostCurrent = vo.getInAmout();
//            BigDecimal realCostUpper = BigDecimal.ZERO;
//
//            BigDecimal standardCostCurrent = vo.getOutAmout();
//            BigDecimal standardCostUpper = BigDecimal.ZERO;
//
//            BigDecimal prolossCostCurrent = vo.getProlossAmout();
//            BigDecimal prolossCostUpper = BigDecimal.ZERO;
//
//            BigDecimal prolossRateCurrent = CalculatedValueUtil.divisionFloat(vo.getProlossAmout(), vo.getOutAmout());
//            BigDecimal prolossRateUpper = BigDecimal.ZERO;
//
//            if (upperPeriod !=null && !upperVoList.isEmpty()) {
//                TqlStapProloss upperVoChild = upperVoList.stream().filter(obj -> obj.getStapCode().equals(vo.getStapCode())).findFirst().orElse(null);
//                if (upperVoChild !=null) {
//                    realCostUpper = realCostUpper.add(upperVoChild.getInAmout());
//                    standardCostUpper = standardCostUpper.add(upperVoChild.getOutAmout());
//                    prolossCostUpper = prolossCostUpper.add(upperVoChild.getProlossAmout());
//                    prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(upperVoChild.getProlossAmout(), upperVoChild.getOutAmout()));
//                }
//            }
//            data.setStapType(vo.getStapType());
//            data.setDeptCode(vo.getDeptCode());
//            data.setStapCode(vo.getStapName());
//            data.setPName(vo.getStapName());
//            data.setRealCostCurrent(realCostCurrent);
//            data.setRealCostUpper(realCostUpper);
//            data.setStandardCostCurrent(standardCostCurrent);
//            data.setStandardCostUpper(standardCostUpper);
//            data.setProlossCostCurrent(prolossCostCurrent);
//            data.setProlossCostUpper(prolossCostUpper);
//            data.setProlossRateCurrent(prolossRateCurrent);
//            data.setProlossRateUpper(prolossRateUpper);
//            datas.add(data);
//        }
//        return datas;
    }

    @Override
    public List<TopCateStapProlossVO> selectDeptStapPage(Map<String, Object> params) {
        String pandianCode= (String) params.get("pandianCode");
        String stapType= (String) params.get("stapType" );
        String deptCode= (String) params.get("deptCode");
        String fnumber = StringUtils.getPandianCodeInfo(pandianCode, 1);
        List<TopCateStapProlossVO> thisList= commonMapper.findProlossGroupStapDept(pandianCode,fnumber,stapType,deptCode);
        if(CollectionUtils.isEmpty(thisList)) return null;
        List<TopCateStapProlossVO> lastList=null;
        String lastPandianCode=commonMapper.findLastPandianCode(pandianCode);
        if(!Objects.isNull(lastPandianCode))
        {
            lastList= commonMapper.findProlossGroupStapDept  (lastPandianCode,fnumber,stapType,deptCode );
        }
        if(!CollectionUtils.isEmpty(lastList))
        {
            for(TopCateStapProlossVO thisVo:thisList)
            {
                String thisDeptCode= thisVo.getDeptCode();
                for(TopCateStapProlossVO lastVo:lastList)
                {
                    if(thisDeptCode.equals(lastVo.getDeptCode()))
                    {
                        thisVo.setRealCostUpper(lastVo.getRealCostCurrent());
                        thisVo.setStandardCostUpper(lastVo.getStandardCostCurrent());
                        thisVo.setProlossCostUpper(lastVo.getProlossCostCurrent());
                    }
                }
            }
        }
        return thisList;

        /*TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());

        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);

        List<TqlStapProloss> upperVoList;
        if (upperPeriod !=null) {
            upperVoList = baseMapper.selectListByStapType(params);
        } else {
            upperVoList = new ArrayList<>();
        }
        if (voList ==null) {
            throw new ServiceException("该门店暂无盘点信息！");
        }
        List<TopCateStapProlossVO> datas = new ArrayList<>();
        Map<String, List<TqlStapProloss>> groupedMap= voList.stream().collect(Collectors.groupingBy(TqlStapProloss::getDeptCode));
        groupedMap.forEach((deptCode, stapTypeGroup) -> {
            TopCateStapProlossVO data = new TopCateStapProlossVO();

            BigDecimal realCostCurrent = BigDecimal.ZERO;
            BigDecimal realCostUpper = BigDecimal.ZERO;

            BigDecimal standardCostCurrent = BigDecimal.ZERO;
            BigDecimal standardCostUpper = BigDecimal.ZERO;

            BigDecimal prolossCostCurrent = BigDecimal.ZERO;
            BigDecimal prolossCostUpper = BigDecimal.ZERO;

            BigDecimal prolossRateCurrent = BigDecimal.ZERO;
            BigDecimal prolossRateUpper = BigDecimal.ZERO;
            //实际金额
            for (TqlStapProloss compute : stapTypeGroup) {
                realCostCurrent = realCostCurrent.add(compute.getInAmout());
                standardCostCurrent = standardCostCurrent.add(compute.getOutAmout());
                prolossCostCurrent = prolossCostCurrent.add(compute.getProlossAmout());
                prolossRateCurrent = prolossRateCurrent.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
            }
            if (upperPeriod !=null && !upperVoList.isEmpty()) {
                List<TqlStapProloss> upperVoListChild = upperVoList.stream().filter(x->x.getDeptCode().equals(deptCode)).collect(Collectors.toList());
                if (!upperVoListChild.isEmpty()) {
                    for (TqlStapProloss compute : upperVoListChild) {
                        realCostUpper = realCostUpper.add(compute.getInAmout());
                        standardCostUpper = standardCostUpper.add(compute.getOutAmout());
                        prolossCostUpper = prolossCostUpper.add(compute.getProlossAmout());
                        prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
                    }
                }
            }
            data.setDeptCode(deptCode);
            data.setPName(baseMapper.getDeptName(deptCode));
            data.setRealCostCurrent(realCostCurrent);
            data.setRealCostUpper(realCostUpper);
            data.setStandardCostCurrent(standardCostCurrent);
            data.setStandardCostUpper(standardCostUpper);
            data.setProlossCostCurrent(prolossCostCurrent);
            data.setProlossCostUpper(prolossCostUpper);
            data.setProlossRateCurrent(prolossRateCurrent);
            data.setProlossRateUpper(prolossRateUpper);
            datas.add(data);
        });
        return datas;*/
    }

    @Override
    public List<TopCateStapProlossVO> selectdeptCatePage(Map<String, Object> params) {
        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());

        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);

        List<TqlStapProloss> upperVoList;
        if (upperPeriod !=null) {
            upperVoList = baseMapper.selectListByStapType(params);
        } else {
            upperVoList = new ArrayList<>();
        }
        if (voList ==null) {
            throw new ServiceException("该门店暂无盘点信息！");
        }
        List<TopCateStapProlossVO> datas = new ArrayList<>();
        TopCateStapProlossVO data;
        for (TqlStapProloss vo : voList) {
            data = new TopCateStapProlossVO();
            BigDecimal realCostCurrent = vo.getInAmout();
            BigDecimal realCostUpper = BigDecimal.ZERO;

            BigDecimal standardCostCurrent = vo.getOutAmout();
            BigDecimal standardCostUpper = BigDecimal.ZERO;

            BigDecimal prolossCostCurrent = vo.getProlossAmout();
            BigDecimal prolossCostUpper = BigDecimal.ZERO;

            BigDecimal prolossRateCurrent = CalculatedValueUtil.divisionFloat(vo.getProlossAmout(), vo.getOutAmout());
            BigDecimal prolossRateUpper = BigDecimal.ZERO;

            if (upperPeriod !=null && !upperVoList.isEmpty()) {
                TqlStapProloss upperVoChild = upperVoList.stream().filter(obj -> obj.getStapCode().equals(vo.getStapCode())).findFirst().orElse(null);
                if (upperVoChild !=null) {
                    realCostUpper = realCostUpper.add(upperVoChild.getInAmout());
                    standardCostUpper = standardCostUpper.add(upperVoChild.getOutAmout());
                    prolossCostUpper = prolossCostUpper.add(upperVoChild.getProlossAmout());
                    prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(upperVoChild.getProlossAmout(), upperVoChild.getOutAmout()));
                }
            }
            data.setStapType(vo.getStapType());
            data.setDeptCode(vo.getDeptCode());
            data.setStapCode(vo.getStapCode());
            data.setPName(vo.getStapName());
            data.setRealCostCurrent(realCostCurrent);
            data.setRealCostUpper(realCostUpper);
            data.setStandardCostCurrent(standardCostCurrent);
            data.setStandardCostUpper(standardCostUpper);
            data.setProlossCostCurrent(prolossCostCurrent);
            data.setProlossCostUpper(prolossCostUpper);
            data.setProlossRateCurrent(prolossRateCurrent);
            data.setProlossRateUpper(prolossRateUpper);
            datas.add(data);
        }
        return datas;
    }

    @Override
    public List<TqlStapProlossItem> info(Map<String, Object> params) {
        List<TqlStapProlossItem> dataList = baseMapper.listByParams(params);
        //实际数量
        BigDecimal initPrice = BigDecimal.ZERO;
        //实际数量
        BigDecimal inNumAdd = BigDecimal.ZERO;
        //实际金额
        BigDecimal inAmoutAdd = BigDecimal.ZERO;
        for (TqlStapProlossItem compute : dataList) {
            if (compute.getType() == 1 || compute.getType() == 2 || compute.getType() == 3) {
                inNumAdd = inNumAdd.add(compute.getNum());
                inAmoutAdd = inAmoutAdd.add(compute.getAmout());
            } else if (compute.getType() == 4 || compute.getType() == 5) {
                inNumAdd = inNumAdd.subtract(compute.getNum());
                inAmoutAdd = inAmoutAdd.subtract(compute.getAmout());
            }
        }
        TqlStapProlossItem item6 = new TqlStapProlossItem();
        item6.setPrice(initPrice);
        item6.setType(6);
        item6.setPrice(inAmoutAdd.subtract(inNumAdd).setScale(2, RoundingMode.HALF_EVEN));
        item6.setNum(inNumAdd);
        item6.setAmout(inAmoutAdd);
        dataList.add(item6);
        return dataList;
    }

    @Override
    public List<TopCateStapProlossVO> deptPage(Map<String, Object> params) {
        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());

        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);

        List<TqlStapProloss> upperVoList;
        if (upperPeriod !=null) {
            params.replace("pandianCode",upperPeriod.getPandianCode());
            upperVoList = baseMapper.selectListByStapType(params);
        } else {
            upperVoList = new ArrayList<>();
        }
        if (voList ==null) {
            throw new ServiceException("该门店暂无盘点信息！");
        }
        List<TopCateStapProlossVO> datas = new ArrayList<>();
        Map<String, List<TqlStapProloss>> groupedMap= voList.stream()
                .collect(Collectors.groupingBy(TqlStapProloss::getDeptCode));
        groupedMap.forEach((deptCode, deptCodeGroup) -> {
            TopCateStapProlossVO data = new TopCateStapProlossVO();
            BigDecimal realCostCurrent = BigDecimal.ZERO;
            BigDecimal realCostUpper = BigDecimal.ZERO;

            BigDecimal standardCostCurrent = BigDecimal.ZERO;
            BigDecimal standardCostUpper = BigDecimal.ZERO;

            BigDecimal prolossCostCurrent = BigDecimal.ZERO;
            BigDecimal prolossCostUpper = BigDecimal.ZERO;

            BigDecimal prolossRateCurrent = BigDecimal.ZERO;
            BigDecimal prolossRateUpper = BigDecimal.ZERO;
            //实际金额
            for (TqlStapProloss compute : deptCodeGroup) {
                realCostCurrent = realCostCurrent.add(compute.getInAmout());
                standardCostCurrent = standardCostCurrent.add(compute.getOutAmout());
                prolossCostCurrent = prolossCostCurrent.add(compute.getProlossAmout());
                prolossRateCurrent = prolossRateCurrent.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
            }
            if (upperPeriod !=null && !upperVoList.isEmpty()) {
                List<TqlStapProloss> upperVoListChild = upperVoList.stream().filter(x->x.getDeptCode().equals(deptCode)).collect(Collectors.toList());
                if (!upperVoListChild.isEmpty()) {
                    for (TqlStapProloss compute : upperVoListChild) {
                        realCostUpper = realCostUpper.add(compute.getInAmout());
                        standardCostUpper = standardCostUpper.add(compute.getOutAmout());
                        prolossCostUpper = prolossCostUpper.add(compute.getProlossAmout());
                        prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
                    }
                }
            }
            data.setDeptCode(deptCode);
            data.setPName(baseMapper.getDeptName(deptCode));
            data.setRealCostCurrent(realCostCurrent);
            data.setRealCostUpper(realCostUpper);
            data.setStandardCostCurrent(standardCostCurrent);
            data.setStandardCostUpper(standardCostUpper);
            data.setProlossCostCurrent(prolossCostCurrent);
            data.setProlossCostUpper(prolossCostUpper);
            data.setProlossRateCurrent(prolossRateCurrent);
            data.setProlossRateUpper(prolossRateUpper);
            datas.add(data);
        });
        return datas;
    }

    @Override
    public List<TqlStapProlossDiagnosis> diagnosis(Map<String, Object> params) {
        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());
        List<TqlStapProloss> currentDataList = baseMapper.selectListByStapType(params);
        if (currentDataList ==null) {
            throw new ServiceException("该门店暂无盘点信息！");
        }

        List<TqlStapProloss> upperDataList;
        if (upperPeriod !=null) {
            params.replace("pandianCode",upperPeriod.getPandianCode());
            upperDataList = baseMapper.selectListByStapType(params);
        } else {
            upperDataList = new ArrayList<>();
        }
        List<TqlStapProlossDiagnosis> diagnosisList = new ArrayList<>();
        TqlStapProlossDiagnosis diagnosis1 = new TqlStapProlossDiagnosis();
        diagnosis1.setType(1);
        diagnosis1.setDesc("本期利润虚增，上期利润虚减");
        diagnosis1.setAmout(BigDecimal.ZERO);
        diagnosis1.setDeptNum(0);
        diagnosis1.setStapNum(0);
        TqlStapProlossDiagnosis diagnosis2 = new TqlStapProlossDiagnosis();
        diagnosis2.setType(2);
        diagnosis2.setDesc("本期利润虚减，上期利润虚增");
        diagnosis2.setAmout(BigDecimal.ZERO);
        diagnosis2.setDeptNum(0);
        diagnosis2.setStapNum(0);
        TqlStapProlossDiagnosis diagnosis3 = new TqlStapProlossDiagnosis();
        diagnosis3.setType(3);
        diagnosis3.setDesc("产生虚减");
        diagnosis3.setAmout(BigDecimal.ZERO);
        diagnosis3.setDeptNum(0);
        diagnosis3.setStapNum(0);

        BigDecimal init = BigDecimal.ZERO;
        Map<String, Map<String, List<TqlStapProloss>>> groupedMap = currentDataList.stream()
                .collect(Collectors.groupingBy(TqlStapProloss::getDeptCode,
                        Collectors.groupingBy(TqlStapProloss::getStapCode)));
        for (Map.Entry<String, Map<String, List<TqlStapProloss>>> deptCodeEntry : groupedMap.entrySet()) {
            String deptCode = deptCodeEntry.getKey();
            int diagnosis1DeptNum = 0;
            int diagnosis2DeptNum = 0;
            for (Map.Entry<String, List<TqlStapProloss>> stapCodeEntry : deptCodeEntry.getValue().entrySet()) {
                String stapCode = stapCodeEntry.getKey();
                List<TqlStapProloss> stapCodeValues = stapCodeEntry.getValue();
                BigDecimal stapAmoutAll = stapCodeValues.stream().map(TqlStapProloss::getProlossAmout).reduce(BigDecimal.ZERO, BigDecimal::add);
                if (!upperDataList.isEmpty()) {
                    List<TqlStapProloss> upperDataItemList = upperDataList.stream().filter(x ->x.getDeptCode().equals(deptCode) && x.getStapCode().equals(stapCode)).collect(Collectors.toList());
                    if (!upperDataItemList.isEmpty()) {
                        init = upperDataItemList.stream().map(TqlStapProloss::getProlossAmout).reduce(BigDecimal.ZERO, BigDecimal::add);
                    }
                }
                if (stapAmoutAll.compareTo(init) > 0) {
                    diagnosis1.setStapNum(diagnosis1.getStapNum() +1);
                    diagnosis1DeptNum ++;
                }else {
                    diagnosis2.setStapNum(diagnosis2.getStapNum() +1);
                    diagnosis2DeptNum ++;
                }
            }
            diagnosis1.setDeptNum(diagnosis1DeptNum);
            diagnosis2.setDeptNum(diagnosis2DeptNum);
        }
        diagnosisList.add(diagnosis1);
        diagnosisList.add(diagnosis2);
        diagnosisList.add(diagnosis3);
        return diagnosisList;
    }

    @Override
    public List<TqlStapProlossDiagnosisInfo> diagnosisInfo(Map<String, Object> params) {
        int type = Integer.parseInt((String) params.get("type"));
        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());
        //本期数据
        List<TqlStapProloss> currentDataList = baseMapper.selectListByStapType(params);
        if (CollectionUtils.isEmpty(currentDataList)) {
            throw new ServiceException("该门店暂无盘点信息！");
        }
        //上期数据
        List<TqlStapProloss> upperDataList;
        if (upperPeriod !=null) {
            params.replace("pandianCode",upperPeriod.getPandianCode());
            upperDataList = baseMapper.selectListByStapType(params);
        } else {
            upperDataList = new ArrayList<>();
        }
        List<TqlStapProlossDiagnosisInfo> diagnosisList = new ArrayList<>();
        TqlStapProlossDiagnosisInfo diagnosis;
        BigDecimal init = BigDecimal.ZERO;
        Map<String, Map<String, List<TqlStapProloss>>> groupedMap = currentDataList.stream()
                .collect(Collectors.groupingBy(TqlStapProloss::getDeptCode,
                        Collectors.groupingBy(TqlStapProloss::getStapCode)));
        for (Map.Entry<String, Map<String, List<TqlStapProloss>>> deptCodeEntry : groupedMap.entrySet()) {
            String deptCode = deptCodeEntry.getKey();
            diagnosis = new TqlStapProlossDiagnosisInfo();

            int stapNum =0;
            BigDecimal amout = BigDecimal.ZERO;
            for (Map.Entry<String, List<TqlStapProloss>> stapCodeEntry : deptCodeEntry.getValue().entrySet()) {
                String stapCode = stapCodeEntry.getKey();

                List<TqlStapProloss> stapCodeValues = stapCodeEntry.getValue();
                BigDecimal stapAmoutAll = stapCodeValues.stream().map(TqlStapProloss::getProlossAmout).reduce(BigDecimal.ZERO, BigDecimal::add);
                if (!upperDataList.isEmpty()) {
                    List<TqlStapProloss> upperDataItemList = upperDataList.stream().filter(x ->x.getDeptCode().equals(deptCode) && x.getStapCode().equals(stapCode)).collect(Collectors.toList());
                    if (!upperDataItemList.isEmpty()) {
                        init = upperDataItemList.stream().map(TqlStapProloss::getProlossAmout).reduce(BigDecimal.ZERO, BigDecimal::add);
                    }
                }
                if (type ==1) {
                    if (stapAmoutAll.compareTo(init) > 0) {
                        stapNum ++;
                        amout = amout.subtract(init);
                    }
                }else if (type ==2) {
                    if (stapAmoutAll.compareTo(init) < 0) {
                        stapNum ++;
                        amout = init.subtract(amout);
                    }
                }else if (type ==3) {
                    if (stapAmoutAll.compareTo(init) < 0) {
                        stapNum ++;
                    }
                }
            }
            if (stapNum > 0 ){
                diagnosis.setDeptName(baseMapper.getDeptName(deptCode));
                diagnosis.setStapNum(stapNum);
                diagnosis.setAmout(amout);
                diagnosisList.add(diagnosis);
            }
        }


        return diagnosisList;
    }

    @Override
    public List<TqlStapProlossItem> infoItem(Map<String, Object> params) {
        List<TqlStapProlossItem> dataList = baseMapper.listByParams(params);
        //实际数量
        BigDecimal initPrice = BigDecimal.ZERO;
        //实际数量
        BigDecimal inNumAdd = BigDecimal.ZERO;
        //实际金额
        BigDecimal inAmoutAdd = BigDecimal.ZERO;
        for (TqlStapProlossItem compute : dataList) {
            if (compute.getType() == 1 || compute.getType() == 2 || compute.getType() == 3) {
                inNumAdd = inNumAdd.add(compute.getNum());
                inAmoutAdd = inAmoutAdd.add(compute.getAmout());
            } else if (compute.getType() == 4 || compute.getType() == 5) {
                inNumAdd = inNumAdd.subtract(compute.getNum());
                inAmoutAdd = inAmoutAdd.subtract(compute.getAmout());
            }
        }
        TqlStapProlossItem item6 = new TqlStapProlossItem();
        item6.setPrice(initPrice);
        item6.setType(6);
        item6.setPrice(inAmoutAdd.subtract(inNumAdd).setScale(2, RoundingMode.HALF_EVEN));
        item6.setNum(inNumAdd);
        item6.setAmout(inAmoutAdd);
        dataList.add(item6);
        return dataList;
    }

    @Override
    public TqlStapProloss selectSpuDeptPage(Long id) {

        TqlStapProloss dto= tqlStapProlossMapper.selectStapProlossById(id);
        return dto;

//        TqlPandianPeriodVO period = commonMapper.findEnableGenPharse(String.valueOf(params.get("pandianCode")));
//        TqlPandianPeriodVO upperPeriod = commonMapper.findEnableGenPharseUpper(period.getDeptCode(), period.getPcode());
//
//        List<TqlStapProloss> voList = baseMapper.selectListByStapType(params);
//
//        List<TqlStapProloss> upperVoList;
//        if (upperPeriod !=null) {
//            upperVoList = baseMapper.selectListByStapType(params);
//        } else {
//            upperVoList = new ArrayList<>();
//        }
//        if (voList ==null) {
//            throw new ServiceException("该门店暂无盘点信息！");
//        }
//        List<TopCateStapProlossVO> datas = new ArrayList<>();
//        Map<String, Map<String, List<TqlStapProloss>>> groupedMap = voList.stream()
//                .collect(Collectors.groupingBy(TqlStapProloss::getDeptCode,
//                        Collectors.groupingBy(TqlStapProloss::getStapCode)));
//        groupedMap.forEach((deptCode, deptCodeGroup) -> {
//            deptCodeGroup.forEach((stapCode, stapCodeGroup) -> {
//                TopCateStapProlossVO data = new TopCateStapProlossVO();
//
//                BigDecimal realCostCurrent = BigDecimal.ZERO;
//                BigDecimal realCostUpper = BigDecimal.ZERO;
//
//                BigDecimal standardCostCurrent = BigDecimal.ZERO;
//                BigDecimal standardCostUpper = BigDecimal.ZERO;
//
//                BigDecimal prolossCostCurrent = BigDecimal.ZERO;
//                BigDecimal prolossCostUpper = BigDecimal.ZERO;
//
//                BigDecimal prolossRateCurrent = BigDecimal.ZERO;
//                BigDecimal prolossRateUpper = BigDecimal.ZERO;
//
//                BigDecimal prolossInNum = BigDecimal.ZERO;
//                BigDecimal prolossoutNum = BigDecimal.ZERO;
//                BigDecimal prolossNum = BigDecimal.ZERO;
//                BigDecimal prolossAmout = BigDecimal.ZERO;
//                //实际金额
//                for (TqlStapProloss compute : stapCodeGroup) {
//                    data.setPrice(compute.getPrice());
//                    data.setNature("主料");
//                    data.setPriceAttr("均价");
//                    data.setPrice(compute.getPrice());
//                    data.setProlossNum(compute.getProlossNum());
//                    prolossNum = prolossNum.add(compute.getProlossNum());
//                    prolossAmout = prolossAmout.add(compute.getProlossAmout());
//                    prolossInNum = prolossInNum.add(compute.getInAmout());
//                    prolossoutNum = prolossoutNum.add(compute.getOutNum());
//
//                    realCostCurrent = realCostCurrent.add(compute.getInAmout());
//                    standardCostCurrent = standardCostCurrent.add(compute.getOutAmout());
//                    prolossCostCurrent = prolossCostCurrent.add(compute.getProlossAmout());
//                    prolossRateCurrent = prolossRateCurrent.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
//                }
////                BigDecimal upPrice = BigDecimal.ZERO;
////                BigDecimal upNum = BigDecimal.ZERO;
////                BigDecimal upAmout = BigDecimal.ZERO;
//                if (upperPeriod !=null && !upperVoList.isEmpty()) {
//                    List<TqlStapProloss> upperVoListChild = upperVoList.stream().filter(x->x.getDeptCode().equals(deptCode)).collect(Collectors.toList());
//                    if (!upperVoListChild.isEmpty()) {
//                        for (TqlStapProloss compute : upperVoListChild) {
//                            realCostUpper = realCostUpper.add(compute.getInAmout());
//                            standardCostUpper = standardCostUpper.add(compute.getOutAmout());
//                            prolossCostUpper = prolossCostUpper.add(compute.getProlossAmout());
//                            prolossRateUpper = prolossRateUpper.add(CalculatedValueUtil.divisionFloat(compute.getProlossAmout(), compute.getOutAmout()));
//                        }
//                    }
//                }
//                data.setInNum(prolossInNum);
//                data.setDeptCode(deptCode);
//                data.setDName(baseMapper.getDeptName(deptCode));
//                data.setStapCode(stapCode);
//                data.setPName(baseMapper.getStapName(stapCode));
//                data.setRealCostCurrent(realCostCurrent);
//                data.setRealCostUpper(realCostUpper);
//                data.setStandardCostCurrent(standardCostCurrent);
//                data.setStandardCostUpper(standardCostUpper);
//                data.setProlossCostCurrent(prolossCostCurrent);
//                data.setProlossCostUpper(prolossCostUpper);
//                data.setProlossRateCurrent(prolossRateCurrent);
//                data.setProlossRateUpper(prolossRateUpper);
//                datas.add(data);
//            });
//        });
//        return datas;
    }

    @Override
    public List<TqlStapProlossDiagnosisInfo> stapProlossAnalys(String pandianCode) {
        return baseMapper.stapProlossAnalys(pandianCode);
    }

    @Override
    public   List<TqlStapProloss> stapProlossAnalysDetail(Long[] idArr) {
        return baseMapper.stapProlossAnalysDetail(idArr);
    }

    @Override
    public ProlossDetialDto findStapProlossDetailByPid(Long id) {
        return baseMapper.findStapProlossDetailByPid(id);
    }

    @Override
    public ProlossDetialDto infoDetial(Map<String, Object> params) {
        return baseMapper.getProlossDetialDto(params);
    }

    @Override
    public TqlStapProloss findStapProloss(Long id) {
        TqlStapProloss dto=  baseMapper.selectById(id);
        return dto;
    }

    @Override
    @Slave
    public List<IncaseDto> findInfo(String incaseIds, Integer month, int flag) {
     String tabName= getTableName(month,flag);
     String[] strArr=incaseIds.split(",");
        Long[] longArr = Arrays.stream(strArr)
                .map(Long::valueOf)
                .toArray(Long[]::new);
       List<IncaseDto> list= commonMapper.findInfo(longArr,tabName,flag);
        return list;
    }

    @Override
    @Master
    public IncaseDto findPandianById(Long thisPandianId) {
        return commonMapper.findPandianById(thisPandianId);
    }

    private String getTableName(Integer month, int flag) {
        String tabName = null;
        switch (flag) {
            case 1:
                tabName = CommonUtil.TABLENAME_TQL_MATARIAL_INCASE + month;
                break;
            case 2:
                tabName = CommonUtil.TABLENAME_TQL_KINGDEE_TRANSFER + month;
                break;
            case 3:
                tabName = CommonUtil.TABLENAME_TQL_KINGDEE_TRANSFER + month;
                break;
            case 4:
                tabName = CommonUtil.TABLENAME_TQL_MATARIAL_PURCHASE + month;
                break;
        }
        return tabName;
    }


}




